var isDescriptionShow = false;
$(function() {
    $('.bkhi_arrow_description img').attr({'src': '/travis/web/images/arrow_down.png'});
    $('.bkhi_full_description').hide();

    $('.bkhi_arrow_description').on('click', function() {
        if (isDescriptionShow) {
            isDescriptionShow = false;
            $('.bkhi_arrow_description img').attr({'src': '/travis/web/images/arrow_down.png'});
            $('.bkhi_full_description').slideUp();
        } else {
            isDescriptionShow = true;
            $('.bkhi_arrow_description img').attr({'src': '/travis/web/images/arrow_up.png'});
            $('.bkhi_full_description').slideDown();
        }
    });

    $('.booking_payment_button').on('click', function() {
        booking();
    });
});

function booking() {
    var isValid = true;

    if (!$('.booking_rooms_special_checkbox').is(':checked')) {
        isValid = false;
        $('.booking_special_condition_p').css({'color': '#ff0000'});
    } else {
        $('.booking_special_condition_p').css({'color': '#6d6d6d'});
    }

    if (!$('.booking_rooms_terms_checkbox').is(':checked')) {
        isValid = false;
        $('.booking_terms_condition_p').css({'color': '#ff0000'});
    } else {
        $('.booking_terms_condition_p').css({'color': '#6d6d6d'});
    }

    if (!$('.booking_rooms_plicy_checkbox').is(':checked')) {
        isValid = false;
        $('.booking_policy_p').css({'color': '#ff0000'});
    } else {
        $('.booking_policy_p').css({'color': '#6d6d6d'});
    }

    if ($('.booking_room_guest_first_name').val() == '' || $('.booking_room_guest_first_name').val() == null) {
        isValid = false;
        $('.booking_room_guest_first_name').css({'border-color': '#ff0000'});
    } else {
        $('.booking_room_guest_first_name').css({'border-color': '#6d6d6d'});
    }

    if ($('.booking_room_guest_second_name').val() == '' || $('.booking_room_guest_second_name').val() == null) {
        isValid = false;
        $('.booking_room_guest_second_name').css({'border-color': '#ff0000'});
    } else {
        $('.booking_room_guest_second_name').css({'border-color': '#6d6d6d'});
    }

    if (isValid) {
        var rooms = '';
        var tokens = '';
        var first_names = '';
        var last_names = '';
        var guest_title = '';
        var passports = '';
        var boards = '';
        var smoking = '';
        var beds = '';

        $('.booking_rooms_description').each(function(i, v) {
            if ($(v).attr('room_id')) {
                rooms = rooms + $(v).attr('room_id') + ';';
            }

            if ($(v).attr('token')) {
                tokens = tokens + $(v).attr('token') + ';';
            }
        });

        $('.booking_room_guest').each(function(i, v) {
            guest_title = guest_title + $(v).find('select.booking_room_quest_status').val() + ';';
            first_names = first_names + $(v).find('input.booking_room_guest_first_name').val() + ';';
            last_names = last_names + $(v).find('input.booking_room_guest_second_name').val() + ';';
            passports = passports + $(v).find('input.booking_room_quest_passport').val() + ';';
        });

        $('.booking_room_data').each(function(i, v) {
            boards = boards + $(v).find('div.booking_room_update select').val() + ';';
            smoking = smoking + $(v).find('div.booking_room_smoke_radio input[name=group1]:checked').val() + ';';
            beds = beds + $(v).find('div.booking_room_bed_radio input[name=group2]:checked').val() + ';';
        });

        var data = {
            hotel_id: $('.bkhi_title').attr('hotel_id'),
            rooms_ids: rooms,
            price: $('.bkhi_total_sum').attr('price'),
            sessions: tokens,
            guest_title: guest_title,
            first_name: first_names,
            last_name: last_names,
            passport: passports,
            date_in: $('#booking_check_in_input').val(),
            days: $('#booking_check_input_night').val(),
            board: boards,
            smoking: smoking,
            bed: beds,
            payment: $('.input_radio_div input[name=group_payment]:checked').val()
        };

        $.ajax({
            type: 'POST',
            url: urlBackend + 'requestbooking',
            data: data,
            beforeSend: function() {
                showLoaderBooking();
            },
            success: function(msg) {
                var json = $.parseJSON(msg);
                console.log(msg);
                $(json).each(function(i, v) {
                    if (v.status === 'success') {
                        hideLoaderBooking();
                        window.location.assign(urlVaucher + '/order/' + v.data);
                    } else {
                        hideLoaderBooking();
                        console.log(v);
                    }
                });
            }
        });
    } else {
        alert('not valid');
    }
}

function showLoaderBooking() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 300;
    var disWidth = (winWidth - boxWidth) / 2;
    var disHeight = (winHeight - 125) / 2;
    $('#show_loader').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('#show_loader').show();
}

function hideLoaderBooking() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('#show_loader').hide();
}


